package siwi.map.android;

import siwi.map.android.Login.LoginFailureHandler;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.wmw.Animation.EnlargeAnimationOut;
import com.wmw.Animation.ShrinkAnimationOut;
import com.wmw.Animation.ViewAction;
import com.wmw.DataPool.CommonData;
import com.wmw.Utility.Common;
import com.wmw.Utility.Encryption;
import com.wmw.Utility.MSG;
import com.wmw.Utility.RegularExpression;
import com.wmw.Utility.SRConstant;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyPassword extends SherlockActivity implements OnClickListener {
	private InputBox _username, _oldPassword, _password, _ensurePassword;
	private EditText view_userName;
	private EditText view_password;
	private EditText view_passwordConfirm;
	private Button view_submit;
	private Button view_clearAll;
	private static final int MENU_EXIT = Menu.FIRST - 1;
	private static final int MENU_ModifyPassword = Menu.FIRST;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modify_password);
		
		initActionBar();
		
		initViews();
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ViewAction.startSpringMenuAnimations(v);
//
//		if (!_username.isValide()) {
//			Toast.makeText(ModifyPassword.this, "请输入正确的用户名",
//					Toast.LENGTH_SHORT).show();
//			return;
//		}

		if (!_password.isValide()) {
			Toast.makeText(ModifyPassword.this, "请输入正确的密码",
					Toast.LENGTH_SHORT).show();
			return;
		}

		if (!_ensurePassword.getContent().equals(_password.getContent())) {
			Toast.makeText(ModifyPassword.this, "两次密码不一致",
					Toast.LENGTH_SHORT).show();
			return;
		}

		// TODO
		Common.toastInfo(ModifyPassword.this, "通知修改密码");
		finish();
	}
	private void initActionBar() {
		// Custom action bar.
		ActionBar actionBar = getSupportActionBar();
		ActionBar.LayoutParams params = new ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
				Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(
				R.layout.action_bar_back_save, null);
		actionBar.setCustomView(view, params);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);

		view.findViewById(R.id.ABBS_Save).setVisibility(View.GONE);
		
		findViewById(R.id.ABBS_Back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewAction.startSpringMenuAnimations(v);
				// Go back.
				Log.d(Common.GetLineInfo(), "Go back without saving");
				goBack();
				return;
			}
		});
		
		TextView tv = (TextView) view.findViewById(R.id.ABBS_Title);
		tv.setText("修改密码");
	}
	private void initViews(){
		SharedPreferences share = getSharedPreferences(SRConstant._SR_Login, 0);
		String userName = share.getString(SRConstant._L_Username, "");
		
		final EditText etUsername = (EditText) findViewById(R.id.MP_Username);
		try {
			etUsername.setText(Encryption.decryptAES(Encryption._Seed, userName));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		final EditText etOldPassword = (EditText) findViewById(R.id.MP_OldPassword);
		ImageView clearOldPassword = (ImageView) findViewById(R.id.MP_ClearOldPassword);
		_oldPassword = new InputBox(this, etOldPassword, clearOldPassword, null,
				etOldPassword.getPaddingRight(), RegularExpression.Password,
				new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etOldPassword
									.setBackgroundColor(ModifyPassword.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etOldPassword
									.setBackgroundColor(ModifyPassword.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});
		
		final EditText etPassword = (EditText) findViewById(R.id.MP_Password);
		ImageView clearPassword = (ImageView) findViewById(R.id.MP_ClearPassword);
		_password = new InputBox(this, etPassword, clearPassword, null,
				etPassword.getPaddingRight(), RegularExpression.Password,
				new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etPassword
									.setBackgroundColor(ModifyPassword.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etPassword
									.setBackgroundColor(ModifyPassword.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		final EditText etEnsurePassword = (EditText) findViewById(R.id.MP_EnsurePassword);
		ImageView clearEnsurePassword = (ImageView) findViewById(R.id.MP_ClearEnsurePassword);
		_ensurePassword = new InputBox(this, etEnsurePassword,
				clearEnsurePassword, null, etEnsurePassword.getPaddingRight(),
				RegularExpression.Password, new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etEnsurePassword
									.setBackgroundColor(ModifyPassword.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etEnsurePassword
									.setBackgroundColor(ModifyPassword.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		Button button = (Button) findViewById(R.id.MP_Sure);
		button.setOnClickListener(this);
	}
	
	private void goBack() {
		Intent intent = new Intent();
		intent.setClass(ModifyPassword.this, Home.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
		ModifyPassword.this.finish();
	}
}
